<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">

    <!-- 通用查询映射结果 -->
    <sql id="${cfg.className}Columns">
        <#list table.fields as field>
        a.${field.name} AS ${field.propertyName},
        </#list>
        <#list table.commonFields as field>
        <#if field_index == table.commonFields?size-1>
        a.${field.name} AS ${field.propertyName}
        <#else>
        a.${field.name} AS ${field.propertyName},
        </#if>
        </#list>
    </sql>

    <sql id="${cfg.className}Joins">
    </sql>

    <insert id="insert">
      INSERT INTO ${table.name}(
      <#list table.fields as field >
          ${field.name},
          </#list>
          <#list table.commonFields as field >
          <#if !field.keyFlag>
          <#if field_index == table.commonFields?size-1>
          ${field.name}
          <#else>
          ${field.name},
          </#if>
          </#if>
      </#list>
      )
      values(
        <#list table.fields as field >
          <#noparse>#{</#noparse>${field.propertyName}<#noparse>}</#noparse>,
        </#list>
        <#list table.commonFields as field>
        <#if !field.keyFlag>
        <#if field_index == table.commonFields?size-1>
          <#noparse>#{</#noparse>${field.propertyName}<#noparse>}</#noparse>
        <#else>
          <#noparse>#{</#noparse>${field.propertyName}<#noparse>}</#noparse>,
        </#if>
        </#if>
        </#list>
      )
    </insert>

    <update id="update">
        UPDATE ${table.name} SET
        <#list table.fields as field >
        <#--<if test="${field.propertyName} != null and ${field.propertyName} != '' ">-->
        ${field.name}=<#noparse>#{</#noparse>${field.propertyName}<#noparse>}</#noparse>,
        <#--</if>-->
        </#list>
        <#list table.commonFields as field>
        <#if !field.keyFlag>
        <#--<if test="${field.propertyName} != null and ${field.propertyName} != '' ">-->
        <#if field_index == table.commonFields?size-1>
        ${field.name}=<#noparse>#{</#noparse>${field.propertyName}<#noparse>}</#noparse>
        <#else>
        ${field.name}=<#noparse>#{</#noparse>${field.propertyName}<#noparse>}</#noparse>,
        </#if>
        <#--</if>-->
        </#if>
        </#list>
        WHERE id = <#noparse>#{</#noparse>id<#noparse>}</#noparse>
    </update>

    <update id="delete">
        UPDATE ${table.name} set
        deleted_by = 1
        WHERE id = <#noparse>#{</#noparse>id<#noparse>}</#noparse>
    </update>

    <select id="get" resultType="${table.entityName}">
        select
        <include refid="${cfg.className}Columns"/>
        from ${table.name} a
        <include refid="${cfg.className}Joins"/>
        WHERE a.id = <#noparse>#{</#noparse>id<#noparse>}</#noparse>
    </select>

    <select id="findList" resultType="${table.entityName}">
        SELECT
        <include refid="${cfg.className}Columns"/>
        FROM ${table.name} a
        <include refid="${cfg.className}Joins"/>
        WHERE a.deleted_by = 0
    </select>

</mapper>